package com.scm.dto.resp.invent;

import com.scm.common.utils.NumUtils;
import com.scm.dto.resp.product.ScmProductDo;
import com.scm.persist.model.ScmInventoryStashProduct;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 库存商品总和数据
 */
@Data
public class ScmInventoryProductStatDo {

    private Integer stashCount;

    private List<ScmInventoryStashProduct> stashProduct;

    private Long productId;

    private Integer productCount;

    private Integer productLockCount;

    private Integer availableCount;

    //单库最大可用库存
    private Integer singleStashAvailableCount = 0;


    public Integer getAvailableCount(){

        if(productCount==null){
            return 0;
        }
        else if(productLockCount==null){
            return productCount;
        }else{
            return productCount-productLockCount;
        }
    }


    public void addScmInventoryStashProduct(ScmInventoryStashProduct scmInventoryStashProduct){

        if(CollectionUtils.isEmpty(stashProduct)){
            stashProduct= Lists.newArrayList();
        }
        stashProduct.add(scmInventoryStashProduct);

        this.setStashCount(stashProduct.size());
        this.setProductId(scmInventoryStashProduct.getProductId());
        this.setProductCount(NumUtils.sum(scmInventoryStashProduct.getProductCount(),this.getProductCount()));
        this.setProductLockCount(NumUtils.sum(scmInventoryStashProduct.getProductLockCount(),this.getProductLockCount()));

        Integer singleStashAvailableCount = 0 ;
        if(scmInventoryStashProduct.getProductCount()==null){
            singleStashAvailableCount = 0;
        } else if(scmInventoryStashProduct.getProductLockCount()==null){
            singleStashAvailableCount = scmInventoryStashProduct.getProductCount();
        }else{
            singleStashAvailableCount = scmInventoryStashProduct.getProductCount()-scmInventoryStashProduct.getProductLockCount();
        }

        if(singleStashAvailableCount>this.getSingleStashAvailableCount()){
            this.setSingleStashAvailableCount(singleStashAvailableCount);
        }

    }

}
